package com.huluobo.high3demo.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.high3demo.R;
import com.huluobo.high3demo.databinding.ActivityRegisterBinding;
import com.huluobo.mvvm.baen.UserBeanResult;
import com.huluobo.mvvm.view.BaseMVVMActivity;

public class RegisterActivity extends BaseMVVMActivity<ActivityRegisterBinding, RegisterViewModel> {

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<UserBeanResult.UserBean>() {
            @Override
            public void onChanged(UserBeanResult.UserBean userBean) {
                ToastUtils.showLong("注册成功");
                finish();
            }
        });

        viewModel.failed.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong("注册失败:" + s);
            }
        });
    }

    @Override
    protected void initView() {
        dataBinding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = dataBinding.edRegisterUsername.getText().toString();
                String password = dataBinding.edRegisterPassword.getText().toString();
                String rePassword = dataBinding.edRegisterRePassword.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(rePassword)) {
                    ToastUtils.showLong("请输入用户名密码");
                    return;
                }
                viewModel.register(username, password, rePassword);
            }
        });
    }

    @Override
    protected Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }
}